package tools;

import java.util.HashMap;
import java.util.Vector;


import agent.FragmentAgent;

import main.Main;

public class FragmentDirectory {

	private HashMap<String, Fragment> listeFragment = new HashMap<String, Fragment>();
	
	public synchronized void addFragment(Fragment frag){
		if (!listeFragment.containsKey(frag.getId()));{
			listeFragment.put(frag.toString(),frag);
			
			Debug.println("Fragment Directory : adding fragment " + frag.getId());
		}
	}
	
	public synchronized void removeFragment(Fragment frag){
		listeFragment.remove(frag);
		
		Debug.println("Fragment Directory : removing fragment " + frag.getId());
	}
	
	public synchronized Vector<Fragment> getFragmentList(){
		return new Vector<Fragment>(listeFragment.values());
	}

	public synchronized boolean nobodyNeed(String mmme) {
		
		if(mmme.equals(Main.finalElement)) 
			return false;
		else{
			boolean res = true;
			for(Fragment f : listeFragment.values())
				if(f.getRequiredElements().contains(mmme) 
						)//&& !f.currentState().equals(AgentState.INACTIVE)) 
					res=false;
			return res;
		}
	}
	
	public synchronized Fragment getFragment(String id){
		return listeFragment.get(id);
	}
}
